package com.weipu.common.facade.model;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.weipu.common.constants.ServerConstant;

/**
 * @author Tony
 * 物流包裹
 */
public class Package implements Serializable{
	private static final long serialVersionUID = -5537753600303281811L;
	private String packageNumber;//包裹ID、批次
	private String trackingNumber;//跟踪号
	private boolean isTracking;//IsTracking=true，挂号。IsTracking=false，平邮
	private ArrayList<Lines> products;//包裹内的产品信息
	private ArrayList<PackageTrace> packageTraces;//物流信息
	private PackageTrace lastPackageTrace;//最新的一条物流信息
	
	

	public String getTrackingNumber() {
		return trackingNumber;
	}


	public void setTrackingNumber(String trackingNumber) {
		this.trackingNumber = trackingNumber;
	}


	public PackageTrace getLastPackageTrace() {
		return lastPackageTrace;
	}


	public void setLastPackageTrace(PackageTrace lastPackageTrace) {
		this.lastPackageTrace = lastPackageTrace;
	}


	public String getPackageNumber() {
		return packageNumber;
	}


	public void setPackageNumber(String packageNumber) {
		this.packageNumber = packageNumber;
	}

	public boolean isTracking() {
		return isTracking;
	}


	public void setTracking(boolean isTracking) {
		this.isTracking = isTracking;
	}


	public ArrayList<Lines> getProducts() {
		return products;
	}


	public void setProducts(ArrayList<Lines> products) {
		this.products = products;
	}


	public ArrayList<PackageTrace> getPackageTraces() {
		return packageTraces;
	}


	public void setPackageTraces(ArrayList<PackageTrace> packageTraces) {
		this.packageTraces = packageTraces;
	}

	public static Package json2Object(JSONObject object){
		Package pack = null;
		if(null != object){
			pack = new Package();
			pack.setPackageNumber(object.optString(ServerConstant.Package.PACKAGES_PACKAGENUMBER));
			pack.setTrackingNumber(object.optString(ServerConstant.Package.PACKAGES_TRACKINGNUM));
			pack.setTracking(object.optBoolean(ServerConstant.Package.PACKAGES_ISTRACKING));
			JSONArray produtsArray = object.optJSONArray(ServerConstant.Package.PACKAGES_PRODUCTS);
			JSONArray packageTracesArray = object.optJSONArray(ServerConstant.Package.PACKAGES_PACKAGETRACES);
			if(produtsArray!=null && produtsArray.length()>0){
				ArrayList<Lines> products = new ArrayList<Lines>();
				for(int i = 0;i<produtsArray.length();i++){
					Lines line = new Lines();
					line = line.json2Object(produtsArray.optJSONObject(i));
					if(line!=null){
						products.add(line);
					}
				}
				pack.setProducts(products);
			}
			if(packageTracesArray!=null && packageTracesArray.length()>0){
				ArrayList<PackageTrace> packageTraces = new ArrayList<PackageTrace>();
				for(int i = 0;i<packageTracesArray.length();i++){
					JSONObject jsonObject = packageTracesArray.optJSONObject(i);
					if(jsonObject!=null){
						PackageTrace packageTrace = new PackageTrace();
						packageTrace.setTraceTime(jsonObject.optString(ServerConstant.Package.PACKAGES_TRACETIME));
						packageTrace.setTrace(jsonObject.optString(ServerConstant.Package.PACKAGES_TRACE));
						packageTraces.add(packageTrace);
					}
				}
				pack.setPackageTraces(packageTraces);
			}
			
			JSONObject lastPackageTraceObj = object.optJSONObject(ServerConstant.Package.PACKAGES_LASTPACKAGETRACE);
			if(lastPackageTraceObj!=null){
				PackageTrace lastPackageTrace = new PackageTrace();
				lastPackageTrace.setTrace(lastPackageTraceObj.optString(ServerConstant.Package.PACKAGES_TRACE));
				lastPackageTrace.setTraceTime(lastPackageTraceObj.optString(ServerConstant.Package.PACKAGES_TRACETIME));
				pack.setLastPackageTrace(lastPackageTrace);
			}
		}
		
		return pack;
	}
}
